package tech.aistar.day13;

import tech.aistar.design.singleton.version04.Single04Demo;
import tech.aistar.design.singleton.version05.Single05Demo;

import java.io.*;

/**
 * 本类用来演示: 枚举单例优势 - 防止序列化和反序列化
 *
 * @author: success
 * @date: 2021/3/24 4:00 下午
 */
public class EnumDemo {
    public static void main(String[] args) {
        try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("src/tech/aistar/day13/enum.txt"));
            ObjectInputStream in = new ObjectInputStream(new FileInputStream("src/tech/aistar/day13/enum.txt"))){

            //Single04Demo s1 = Single04Demo.getInstance();

            //out.writeObject(s1);//将该对象序列化

            //Single04Demo s2 = (Single04Demo) in.readObject();

            //System.out.println(s1 == s2);//false

            System.out.println("====");

            //枚举类型
            Single05Demo s1 = Single05Demo.INSTANCE;
            out.writeObject(s1);

            Single05Demo s2 = (Single05Demo) in.readObject();

            System.out.println(s1 == s2);//true

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
